package com.myspring.web.view.zip;

import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 参考：Spring MVC 4 - File download example：https://www.boraji.com/spring-mvc-4-file-download-example
 *
 *
 * Created by Administrator on 2018/3/21.
 */

@Controller
@RequestMapping("/zip")
public class ZipController {

    @RequestMapping(value = "/download", produces = {"application/zip"})
    public ResponseEntity<ByteArrayResource> downloadFile2() throws Exception {
        File file = File.createTempFile("springmvc_zip_", ".zip");

        File xlsFile = File.createTempFile("springmvc_xls_", ".xls");
        FileOutputStream fileOutputStream = new FileOutputStream(xlsFile);
        //临时excel文件写入内容
        writeExcel(fileOutputStream);

        //组装zip实体
        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
        ZipEntry zipEntry = new ZipEntry("a.xls");
        zipOutputStream.putNextEntry(zipEntry);
        //将要压缩的文件写入压缩文件
        FileInputStream fileInputStream = new FileInputStream(xlsFile);
        IOUtils.copy(fileInputStream, zipOutputStream);
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(zipOutputStream);

        Path path = Paths.get(file.getCanonicalPath());
        byte[] data = Files.readAllBytes(path);
        ByteArrayResource resource = new ByteArrayResource(data);


        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment;filename=" + path.getFileName().toString())
                .contentType(MediaType.MULTIPART_FORM_DATA).contentLength(data.length)
                .body(resource);
    }

    public void writeExcel(FileOutputStream fileOutputStream) {
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet();

            HSSFRow row = sheet.createRow(0);
            HSSFCell cell0 = row.createCell(0);
            cell0.setCellValue("hello");
            HSSFCell cell1 = row.createCell(1);
            cell1.setCellValue("world");
            wb.write(fileOutputStream);
            wb.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) throws Exception {
        File file = File.createTempFile("springmvc_zip_", ".zip");
        Path path = Paths.get(file.getCanonicalPath());
        byte[] data = Files.readAllBytes(path);
        System.out.println(data);
    }
}
